
package at.fhj.ase.crypto.server.net;

import java.io.IOException;
import java.net.Socket;

import at.fhj.ase.crypto.common.net.MessageHandler;
import at.fhj.ase.crypto.common.net.SocketWrapperThread;

/**
 * Class mainly exists for convenience and easier understanding reasons<br>
 * It represents a connection that this Server has with <strong>one</strong> client.
 * <p>
 * We could also say that the class is another "wrapper" for the {@link SocketWrapperThread} as it automatically instantiates the thread and also starts it.<br>
 * But the main reason is for easier understanding as a {@link SocketWrapperThread} can (and should!) be used on both client and server side, and no body really
 * knows which socket is meant then (can be a client as well as a server socket, concerning the direction).
 * <p>
 * Using this class, it should be clear that a connection from server to client is created, using a {@link SocketWrapperThread} in the background.
 * 
 * @author sfe | www.fh-joanneum.at | Practical Aspects of Cryptography
 */
public class ServerClientConnection {
  /** The instance of the {@link SocketWrapperThread} (e.g. for stopping it again from outside) */
  private final SocketWrapperThread socketWrapper;

  /**
   * Constructor creating and establishing a connection from server to client.<br>
   * An encapsulated {@link SocketWrapperThread} is therefore created and also started.
   * 
   * @param socket the client socket we want to wrap
   * @param messageHandler the {@link MessageHandler} that is needed by the encapsulated {@link SocketWrapperThread} instance
   * @throws IOException if a problem occurs while creating the {@link SocketWrapperThread}
   */
  ServerClientConnection(final Socket socket, final MessageHandler messageHandler) throws IOException {
    socketWrapper = new SocketWrapperThread(socket, messageHandler);
    socketWrapper.start();
  }

  /** @return the instance of the running {@link SocketWrapperThread} (e.g. for stopping it again from outside) */
  final SocketWrapperThread getSocketWrapper() {
    return socketWrapper;
  }
}
